using GZ.Common.Data;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Configuration;
using System.Runtime.Serialization;
using GZ.DB.Map.JT_WCS;
using GZ.DB.Entity.JT_WCS;
using GZ.DB.IRepository.JT_WCS;
using GZ.DB.Repository.JT_WCS;

namespace GZ.Projects.WCS_SVC
{


	/// <summary>
	/// WCS作业任务操作C 响应参数
	/// </summary>
	[Serializable]
	public class ToWmsReportRequest
	{
		public string feed_no;
		public int feed_type;
		public int task_type;
		public string task_no;
		public string mat_code;
		//当前位置
		public string cur_station_no;
		public string feed_time;
	}
	[Serializable]
	public class ToWmsReportResponse
	{
		public string feed_no;
		public int feed_type;
		public int task_type;
		public string task_no;
		public string mat_code;
		//当前位置
		public string cur_station_no;
		public string feed_time;
	}


	[Serializable]
	public class ToWmsCreateOrderData
	{
		public int result_flag;
		public string err_msg;
	}
	[Serializable]
	public class ToWmsCreateOrderRequest
	{
		public string req_no;
		//1=货物入库；2=货物出库；3=托盘组入库；4=托盘组出库；5=移动（不过库位）；6=移库
		public int task_type;
		public string task_no;
		public string pre_task_no;
		public string tunnel_no;
		public string from_pos;
		public string to_pos;
		public string mat_code;
		public string mat_tyoe;
		public string mat_memo;
		public DateTime req_time;
		public int task_pri;
	}
	public static class ToWmsHandle
	{
		/// <summary>
		/// IC01.作业任务创建M
		/// </summary>
		/// <param name="requestJson"></param>
		/// <returns></returns>
		public static string CreateOrder(string requestJson)
		{
			var response = new ToWmsCreateOrderData();
			string respJson = string.Empty;
			try
			{
				var reqList = JsonConvert.DeserializeObject<List<ToWmsCreateOrderRequest>>(requestJson);
				if (reqList == null)
				{
					response.result_flag = (int)HttpStatusCode.InternalServerError;
					response.err_msg = "解析请求报文失败";
					respJson = JsonConvert.SerializeObject(response);
					return respJson;
				}
				List<MainMissionEntity> mainList = new List<MainMissionEntity>();
				foreach (var request in reqList)
				{
					MainMissionEntity mission = new MainMissionEntity();
					mission.order_type = request.task_type.ToString();
					mission.order_no = request.req_no;
					mission.task_type = request.task_type.ToString();
					mission.task_no = request.task_no;
					mission.task_priority = request.task_pri;
					mission.tunnel_no = request.tunnel_no;
					mission.start_position = request.from_pos;
					mission.end_position = request.to_pos;
					mission.tray_code = request.mat_code;
					mission.material_no = request.mat_tyoe;
					mission.material_name = request.mat_memo;
					mission.order_time = request.req_time;
					mission.ope_time = DateTime.Now; ;
					mission.LastUpdatedTime = DateTime.Now;
					mission.pre_task_no = request.pre_task_no;
					mission.MissionState = "Create";
					mission.CreateTime = DateTime.Now;
					mission.MissionStartTime = DateTime.Now;
					mission.InteStationNo = request.from_pos;
					mission.CreateTime = DateTime.Now;
					IMainMissionRepository svc = new MainMissionRepository();
					MainMissionEntity repeat = svc.FindEntity(t => t.task_no == request.task_no);

					if (repeat == null)
					{
						mainList.Add(mission);
					}
					else
					{
						response.result_flag = 1;
						response.err_msg = "任务编号重复";
						respJson = JsonConvert.SerializeObject(response);
						return respJson;
					}

				}
				if (mainList != null && mainList.Count > 0)
				{
					AutoScanApp autosan = new AutoScanApp();
					bool mainbl = autosan.ADDMainMission(mainList, out string mainessmage);
					if (!mainbl)
					{
						response.result_flag = 1;
						response.err_msg = "更新数据失败；" + mainessmage;
						respJson = JsonConvert.SerializeObject(response);
						return respJson;
					}
				}

				response.result_flag =0;
				response.err_msg = "成功";
				respJson = JsonConvert.SerializeObject(response);
				return respJson;
			}
			catch (Exception ex)
			{
				response.result_flag = (int)HttpStatusCode.InternalServerError;
				response.err_msg = "成功";
				respJson = JsonConvert.SerializeObject(response);
				return respJson;
			}

		}

	}
}
